package com.ny.dao.impl;
import com.ny.dao.StudentDao;
import com.ny.entity.Student;
import com.ny.until.DbUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {
    private DbUtil dbUtil = new DbUtil();

    @Override
    public Student selectStudent(String name, String password) throws SQLException {
        Connection connection = dbUtil.getCon();
        String sql = "select * from students where name = ? and password = ?";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        prepareStatement.setString(1, name);
        prepareStatement.setString(2, password);

        ResultSet resultSet = prepareStatement.executeQuery();
        Student student = new Student();

        if (resultSet.next()) {
            int id = resultSet.getInt("student_id");
            String sname = resultSet.getString("name");
            String phone = resultSet.getString("phone");
            String spassword = resultSet.getString("password");

            student.setStudentId(id);
            student.setName(sname);
            student.setPhone(phone);
            student.setPassword(spassword);
            return student;
        }
        else {
            return null;
        }
    }

    @Override
    public int insertStudent(Student student) throws SQLException {
        Connection connection = dbUtil.getCon();
        String sql = "INSERT INTO students(name,phone,password) VALUES (?,?,?);";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        prepareStatement.setString(1, student.getName());
        prepareStatement.setString(2, student.getPassword());
        prepareStatement.setString(3, student.getPhone());
        int i = prepareStatement.executeUpdate();
        return i;
    }
    @Override
    public List<Student> selectAllStudents() throws SQLException {
        List<Student> students = new ArrayList<>();
        String sql = "SELECT * FROM students";

        try (Connection conn = dbUtil.getCon();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                Student student = new Student();
                student.setStudentId(rs.getInt("student_id"));
                student.setName(rs.getString("name"));
                student.setPassword(rs.getString("password"));
                student.setPhone(rs.getString("phone"));
                students.add(student);
            }
        }
        return students;
    }

    @Override
    public Student getStudentById(int studentId) throws SQLException {
        String sql = "SELECT * FROM students WHERE student_id = ?";
        try (Connection conn = dbUtil.getCon();
             PreparedStatement ps = conn.prepareStatement(sql)) {

            ps.setInt(1, studentId);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    Student student = new Student();
                    student.setStudentId(rs.getInt("student_id"));
                    student.setName(rs.getString("name"));
                    student.setPassword(rs.getString("password"));
                    student.setPhone(rs.getString("phone"));
                    return student;
                }
            }
        }
        return null;
    }

    @Override
    public int updateStudent(Student student) throws SQLException {
        String sql = "UPDATE students SET name = ?, password = ?, phone = ? WHERE student_id = ?";
        try (Connection conn = dbUtil.getCon();
             PreparedStatement ps = conn.prepareStatement(sql)) {

            ps.setString(1, student.getName());
            ps.setString(2, student.getPassword());
            ps.setString(3, student.getPhone());
            ps.setInt(4, student.getStudentId());

            return ps.executeUpdate();
        }
    }

    @Override
    public int deleteStudent(int studentId) throws SQLException {
        String sql = "DELETE FROM students WHERE student_id = ?";
        try (Connection conn = dbUtil.getCon();
             PreparedStatement ps = conn.prepareStatement(sql)) {

            ps.setInt(1, studentId);
            return ps.executeUpdate();
        }
    }
}
